# ----------------------------------------------------------------------------
# Title:   Scientific Visualisation - Python & Matplotlib
# Author:  Nicolas P. Rougier
# License: Creative Commons BY-NC-SA International 4.0
# ----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt

# Translated from
# http://www.piclist.com/tecHREF/datafile/charset/extractor/charset_extractor.htm
font = np.array(
    [
        (0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
        (0x10, 0xE3, 0x84, 0x10, 0x01, 0x00),
        (0x6D, 0xB4, 0x80, 0x00, 0x00, 0x00),
        (0x00, 0xA7, 0xCA, 0x29, 0xF2, 0x80),
        (0x20, 0xE4, 0x0C, 0x09, 0xC1, 0x00),
        (0x65, 0x90, 0x84, 0x21, 0x34, 0xC0),
        (0x21, 0x45, 0x08, 0x55, 0x23, 0x40),
        (0x30, 0xC2, 0x00, 0x00, 0x00, 0x00),
        (0x10, 0x82, 0x08, 0x20, 0x81, 0x00),
        (0x20, 0x41, 0x04, 0x10, 0x42, 0x00),
        (0x00, 0xA3, 0x9F, 0x38, 0xA0, 0x00),
        (0x00, 0x41, 0x1F, 0x10, 0x40, 0x00),
        (0x00, 0x00, 0x00, 0x00, 0xC3, 0x08),
        (0x00, 0x00, 0x1F, 0x00, 0x00, 0x00),
        (0x00, 0x00, 0x00, 0x00, 0xC3, 0x00),
        (0x00, 0x10, 0x84, 0x21, 0x00, 0x00),
        (0x39, 0x14, 0xD5, 0x65, 0x13, 0x80),
        (0x10, 0xC1, 0x04, 0x10, 0x43, 0x80),
        (0x39, 0x10, 0x46, 0x21, 0x07, 0xC0),
        (0x39, 0x10, 0x4E, 0x05, 0x13, 0x80),
        (0x08, 0x62, 0x92, 0x7C, 0x20, 0x80),
        (0x7D, 0x04, 0x1E, 0x05, 0x13, 0x80),
        (0x18, 0x84, 0x1E, 0x45, 0x13, 0x80),
        (0x7C, 0x10, 0x84, 0x20, 0x82, 0x00),
        (0x39, 0x14, 0x4E, 0x45, 0x13, 0x80),
        (0x39, 0x14, 0x4F, 0x04, 0x23, 0x00),
        (0x00, 0x03, 0x0C, 0x00, 0xC3, 0x00),
        (0x00, 0x03, 0x0C, 0x00, 0xC3, 0x08),
        (0x08, 0x42, 0x10, 0x20, 0x40, 0x80),
        (0x00, 0x07, 0xC0, 0x01, 0xF0, 0x00),
        (0x20, 0x40, 0x81, 0x08, 0x42, 0x00),
        (0x39, 0x10, 0x46, 0x10, 0x01, 0x00),
        (0x39, 0x15, 0xD5, 0x5D, 0x03, 0x80),
        (0x39, 0x14, 0x51, 0x7D, 0x14, 0x40),
        (0x79, 0x14, 0x5E, 0x45, 0x17, 0x80),
        (0x39, 0x14, 0x10, 0x41, 0x13, 0x80),
        (0x79, 0x14, 0x51, 0x45, 0x17, 0x80),
        (0x7D, 0x04, 0x1E, 0x41, 0x07, 0xC0),
        (0x7D, 0x04, 0x1E, 0x41, 0x04, 0x00),
        (0x39, 0x14, 0x17, 0x45, 0x13, 0xC0),
        (0x45, 0x14, 0x5F, 0x45, 0x14, 0x40),
        (0x38, 0x41, 0x04, 0x10, 0x43, 0x80),
        (0x04, 0x10, 0x41, 0x45, 0x13, 0x80),
        (0x45, 0x25, 0x18, 0x51, 0x24, 0x40),
        (0x41, 0x04, 0x10, 0x41, 0x07, 0xC0),
        (0x45, 0xB5, 0x51, 0x45, 0x14, 0x40),
        (0x45, 0x95, 0x53, 0x45, 0x14, 0x40),
        (0x39, 0x14, 0x51, 0x45, 0x13, 0x80),
        (0x79, 0x14, 0x5E, 0x41, 0x04, 0x00),
        (0x39, 0x14, 0x51, 0x55, 0x23, 0x40),
        (0x79, 0x14, 0x5E, 0x49, 0x14, 0x40),
        (0x39, 0x14, 0x0E, 0x05, 0x13, 0x80),
        (0x7C, 0x41, 0x04, 0x10, 0x41, 0x00),
        (0x45, 0x14, 0x51, 0x45, 0x13, 0x80),
        (0x45, 0x14, 0x51, 0x44, 0xA1, 0x00),
        (0x45, 0x15, 0x55, 0x55, 0x52, 0x80),
        (0x45, 0x12, 0x84, 0x29, 0x14, 0x40),
        (0x45, 0x14, 0x4A, 0x10, 0x41, 0x00),
        (0x78, 0x21, 0x08, 0x41, 0x07, 0x80),
        (0x38, 0x82, 0x08, 0x20, 0x83, 0x80),
        (0x01, 0x02, 0x04, 0x08, 0x10, 0x00),
        (0x38, 0x20, 0x82, 0x08, 0x23, 0x80),
        (0x10, 0xA4, 0x40, 0x00, 0x00, 0x00),
        (0x00, 0x00, 0x00, 0x00, 0x00, 0x3F),
        (0x30, 0xC1, 0x00, 0x00, 0x00, 0x00),
        (0x00, 0x03, 0x81, 0x3D, 0x13, 0xC0),
        (0x41, 0x07, 0x91, 0x45, 0x17, 0x80),
        (0x00, 0x03, 0x91, 0x41, 0x13, 0x80),
        (0x04, 0x13, 0xD1, 0x45, 0x13, 0xC0),
        (0x00, 0x03, 0x91, 0x79, 0x03, 0x80),
        (0x18, 0x82, 0x1E, 0x20, 0x82, 0x00),
        (0x00, 0x03, 0xD1, 0x44, 0xF0, 0x4E),
        (0x41, 0x07, 0x12, 0x49, 0x24, 0x80),
        (0x10, 0x01, 0x04, 0x10, 0x41, 0x80),
        (0x08, 0x01, 0x82, 0x08, 0x24, 0x8C),
        (0x41, 0x04, 0x94, 0x61, 0x44, 0x80),
        (0x10, 0x41, 0x04, 0x10, 0x41, 0x80),
        (0x00, 0x06, 0x95, 0x55, 0x14, 0x40),
        (0x00, 0x07, 0x12, 0x49, 0x24, 0x80),
        (0x00, 0x03, 0x91, 0x45, 0x13, 0x80),
        (0x00, 0x07, 0x91, 0x45, 0x17, 0x90),
        (0x00, 0x03, 0xD1, 0x45, 0x13, 0xC1),
        (0x00, 0x05, 0x89, 0x20, 0x87, 0x00),
        (0x00, 0x03, 0x90, 0x38, 0x13, 0x80),
        (0x00, 0x87, 0x88, 0x20, 0xA1, 0x00),
        (0x00, 0x04, 0x92, 0x49, 0x62, 0x80),
        (0x00, 0x04, 0x51, 0x44, 0xA1, 0x00),
        (0x00, 0x04, 0x51, 0x55, 0xF2, 0x80),
        (0x00, 0x04, 0x92, 0x31, 0x24, 0x80),
        (0x00, 0x04, 0x92, 0x48, 0xE1, 0x18),
        (0x00, 0x07, 0x82, 0x31, 0x07, 0x80),
        (0x18, 0x82, 0x18, 0x20, 0x81, 0x80),
        (0x10, 0x41, 0x00, 0x10, 0x41, 0x00),
        (0x30, 0x20, 0x83, 0x08, 0x23, 0x00),
        (0x29, 0x40, 0x00, 0x00, 0x00, 0x00),
        (0x10, 0xE6, 0xD1, 0x45, 0xF0, 0x00),
    ],
    dtype=np.uint8,
)

text = "The quick brown fox jumps over the lazy dog!"
Z = np.zeros((8, len(text), 6, 4))
for i in range(len(text)):
    Z[:, i, :, -1] = np.unpackbits(font[ord(text[i]) - ord(" ")]).reshape(8, 6)
Z = Z.reshape(8, len(text) * 6, 4)

height, width, _ = Z.shape
dpi = 100
fig_width = width / dpi
fig_height = height / dpi

fig = plt.figure(figsize=(fig_width, fig_height), dpi=dpi)
ax = fig.add_axes([0, 0, 1, 1], frameon=False, xticks=[], yticks=[])
ax.imshow(Z, interpolation="nearest")

plt.savefig(
    "../../figures/anatomy/pixel-size.png", dpi=4 * 1200, transparent=True
)  # dpi/10)
# plt.show()
